package summary;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/31 23:46
 * https://leetcode-cn.com/problems/keys-and-rooms/
 */
public class Title841 {

    private static int OPEN = 1;
    private static int UNOPENED = 0;
    private static int count;

    public static boolean canVisitAllRooms(List<List<Integer>> rooms) {
        int roomSize = rooms.size();
        int[] isOpen = new int[roomSize];
        count = 0;
        dfs(rooms, isOpen, 0);
        return count == roomSize;
    }

    private static void dfs(List<List<Integer>> rooms, int[] isOpen, int cur) {
        isOpen[cur] = OPEN;
        count++;
        if (rooms.get(cur) == null) {
            return;
        }
        for (Integer roomKey : rooms.get(cur)) {
            if(isOpen[roomKey] == UNOPENED){
                dfs(rooms, isOpen, roomKey);
            }
        }
    }

    public static void main(String[] args) {
        List<List<Integer>> rooms = new ArrayList<>();
        List<Integer> list =new ArrayList<>();
        list.add(1);
        rooms.add(list);
        rooms.add(new ArrayList<Integer>() {{
            add(1);
        }});
        boolean b = canVisitAllRooms(rooms);
        System.out.println(b);
    }
}
